<?php
namespace Common\Model;
use Think\Model;
/**
 * 文章Model
 */
class NewsModel extends Model
{

  private $_db = '';
  /**
   * [__construct description]
   */
  public function __construct(){
    $this->_db = M('news');
  }
  /**
   * [addNewNews description]
   * @param array $data [description]
   */
  public function addNews($data = array()){
    $data['create_time'] = time();
    $user = getLoginUserInfo();
    $data['author'] = $user['username'];
    $data['status'] = 1;
    return $this->_db->add($data);
  }
  public function addNewContent($data){
    $this->_db = M('news_content');
    return $this->_db->add($data);
  }
  /**
   * [getNewsCount description]
   * @param  array  $map [description]
   * @return [type]      [description]
   */
  public function getNewsCount($map = array()){
      $map['status'] = array('neq',-1);
      return $this->_db->where($map)->count();
  }
  /**
   * [getNewsList description]
   * @param  [type] $page     [description]
   * @param  [type] $pageSize [description]
   * @param  array  $data     [description]
   * @return [type]           [description]
   */
  public function getNewsList($page,$pageSize,$map = array()){
    $map['status'] = array('neq',-1);
    if(isset($data['keyword'])){
      $map['title'] = array('like','%'.$map['keyword'].'%');
    }
    if(isset($data['type_id'])){
      $map['type_id'] = $map['type_id'];
    }
    $offset = ($page-1)*$pageSize;
    return $this->_db->where($map)->order('news_id')->limit($offset,$pageSize)->select();
  }
  /**
   * [getNewsById description]
   * @param  [type] $id [description]
   * @return [type]     [description]
   */
  public function getNewsById($id){
    $map['news_id']  = $id;
    $map['status'] = array('neq',-1);
    return $this->_db->where($map)->find();
  }
  /**
   * [updateNews description]
   * @param  [type] $id   [description]
   * @param  [type] $data [description]
   * @return [type]       [description]
   */
  public function updateNews($id,$data){
    if(!$id||!is_numeric($id)){
      throw_exception("ID不合法");
    }
    if(!$data||!is_array($data)){
      throw_exception("数据不合法");
    }
    $map['news_id'] = $id;
    return $this->_db->where($map)->save($data);
  }
  public function getNewsContent($id){
    $this->_db = M('news_content');
    $map['news_id'] = $id;
    return $this->_db->where($map)->find();
  }
}
